package javaseof10day.day5.pm;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Toolkit;

import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JTable;
 
public class ClassInfo extends JFrame{
 
	private JPanel contentPane;
	private JTable table;
 
	//launch the application
	public static void main(String[] args){
		
		
		
	
			
			
		
				try {
					//设置外观
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
						| UnsupportedLookAndFeelException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

	

					ClassInfo frame = new ClassInfo();
					frame.setVisible(true);

	}
 
	
	
	
	public ClassInfo(){
		setTitle("显示用户信息");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//可以关闭
		

		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		System.out.println(width);
		
		
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		// 得到窗体的宽、高
		int windowsWidth = this.getWidth();
		System.out.println(windowsWidth);
		
		
		int windowsHeight = this.getHeight();

		this.setBounds((width - windowsWidth) / 2,(height - windowsHeight) / 2, width/2, height/2);
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(10,10,10,10));
		contentPane.setLayout(new BorderLayout(0,0));
		setContentPane(contentPane);
 
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane,BorderLayout.CENTER);
		scrollPane.setViewportView(getTable());  //设置显示在scrollPane中的内容
	}
 
	private JTable getTable(){
		if(table == null){
			table = new JTable();
			table.setRowHeight(23);  //设置行高度
			
			
			

			
			
			DefaultTableModel model=new DefaultTableModel();
			
			
			
			
			table.setModel(model);  //设置表格模型
			
	
			
			model.addColumn("id");
			model.addColumn("name");
			model.addColumn("password");
			
			
			List<Users> userlist = getStudents();
			
	
			
	
			
			
			
			for(int i=0;i< userlist.size();i++){
				
				   Vector<String> vt = new Vector<String>();
				   
				   vt.add(""+userlist.get(i).getId());
				   vt.add(""+userlist.get(i).getName());
				   vt.add(""+userlist.get(i).getPassword());
				
				model.insertRow(i,vt );
			
			}
			
			
			
		}
		return table;
	}
 
	private List<Users> getStudents(){
		List<Users> userlist = new ArrayList<Users>();
		userlist.add(new Users(1,"admin1","admin1"));
		userlist.add(new Users(2,"admin2","admin2"));
		userlist.add(new Users(3,"admin3","admin3"));
		userlist.add(new Users(4,"admin4","admin4"));

		return userlist;
	}
 
}
